// pages/my/merchandise/suit/suit.js
import CommonPage from "../../common/CommonPage.js"

const app = getApp()
const api = require('../../api/http.js')
const config = require('../../../config.js')


class SuitPage extends CommonPage {

  /**
   * 页面的初始数据
   */
  constructor(...args) {
    super(...args)
    super.$init({
      hasSuit: true,
      suitList: {},
      suitMap: {},
      idMap: {},
      height: "0rpx",
      merchList: [],
      showImage: true,
      titleName: "我的背包",
      backImagePath: app.globalData.backImagePath,
      count: '0'
    })
  }
  initData = function(query, resolve, reject) {
    var that = this
    console.log('onSuitPage query:', query)
    that.data.height = app.globalData.titleHeight
    var suitMap = app.globalData.suitMap
    var param = {
      openId: query.openId
    }
    var idList = []
    api.post(config.unsentListUrl, param).then(res => {
      console.debug("Unsent list", res)
      if (res.length != 0) {
        for (var i in res) {
          idList.push(res[i].merchId)
        }
        console.debug("ID List\n", i, idList)
        for (var i in suitMap) {
          var mers = suitMap[i].merchs
          suitMap[i].ocp = 0
          for (var j in mers) {
            var flag = idList.indexOf(mers[j].id) != -1
            if (flag) {
              suitMap[i].ocp += 1
              suitMap[i].merchs[j].flag = true
            }
          }
        }
      }
      that.data.suitMap = suitMap
      that.$setData(that.data)
    })
  }
  $onNavigator(query) {
    console.debug("预加载-礼包列表；参数：",query)
    this.$put('suit-data', this.initData.bind(this), query)

  }
  suitCheck = function() {
    var that = this
    var suitList = {}
    var suitMap = app.globalData.suitMap
    
    api.get(config.suitListUrl).then(res => {
      if (res.length == 0) {
        console.log("礼包列表为空！")
        that.setData({
          hasSuit: false
        })
        return
      }
      console.log("礼包列表：\n", res)
      for (var i in res) {
        suitList[res[i].suitId] = {
          name: res[i].suitName,
          desc: res[i].suitDesc,
          merchCount: res[i].merchCount
        }
      }
      console.log("SuitList:\n", suitList)
      that.setData({
        suitList: suitList,
        height: app.globalData.titleHeight
      })
      for (var i in suitMap) {
        if (suitList[i].name != suitMap[i].name) {
          console.error("Suit info is not consistent!")
        }
      }
    })
  }
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad = function(options) {
    // this.suitCheck()
    var dat = this.$take("suit-data")
    if (dat) return
    this.initData()
  }

  back = function(params) {
    app.back();
  }

  toSuitDetail = function(e) {
    console.debug("礼包跳转参数", e, this)
    var data = e.currentTarget.dataset
    var suitId = data.suitid  //无大小写区别
    var suitMap = this.data.suitMap[suitId]
    var strSuit = JSON.stringify(suitMap)
    var name = data.name
    console.log("SuitMap:", suitMap)
    var query = {
      suitId: suitId,
      suitMap: strSuit,
      name: name
    }
    if (suitMap.ocp < suitMap.merchs.length) {
      wx.showToast({
        title: '您还未集满，需自费邮费哦',
        icon: 'none',
        duration: 1500,
      })
    }
      this.$route({
        path: './detail/suitDetail',
        query: query,
        clazzName: "SuitDetailPage"
      }) 
  }

}

// Page(new SuitPage())
Page(new SuitPage({ clazzName: "SuitPage" }))
